package cn.weiyuanbiao.DbGeneralCore.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Map;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

public class TempleteUtil {

	/**
	 * 根据具体模板生成文件
	 * 
	 * @param templateFileName
	 * @param paraMap
	 * @param filePath
	 */
	public static void createFileByTemplete(String templateFileName, Map<String, Object> paraMap, String filePath) {
		try {
			ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
			Configuration cfg = Configuration.defaultConfiguration();
			GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
			gt.setSharedVars(paraMap);
			Template t = gt.getTemplate(templateFileName);
			String javaSrc = t.render();
			File file = new File(filePath);
			if (file.getParent() != null && !new File(file.getParent()).exists()) {
				new File(file.getParent()).mkdirs();
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(file));
			output.write(javaSrc);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
